import { request, useUserStore, clearEmpty } from "jmash-core";
import type { AxiosPromise } from "axios";
import type {
  TaskAttachmentApi,
  TaskAttachmentReq,
  TaskAttachmentKey,
  TaskAttachmentList,
  TaskAttachmentModel,
  TaskAttachmentCreateReq,
  TaskAttachmentUpdateReq,
} from "./types";

class TaskAttachmentApiImpl implements TaskAttachmentApi {
  getKey(model: TaskAttachmentModel): TaskAttachmentKey {
    return {
      attachmentId: model.id,
    } as TaskAttachmentKey;
  }

  // 查询列表信息
  findList(query: TaskAttachmentReq): AxiosPromise<TaskAttachmentList> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/flow/task/attachment/list/" + tenant,
      method: "get",
      params: query,
    });
  }

  // 创建实体
  create(data: TaskAttachmentCreateReq): AxiosPromise<TaskAttachmentModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/flow/task/attachment",
      method: "post",
      data: data,
    });
  }

  // 修改实体
  update = function (
    data: TaskAttachmentUpdateReq
  ): AxiosPromise<TaskAttachmentModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/flow/task/attachment",
      method: "patch",
      data: data,
    });
  };

  // 删除
  delete(query: TaskAttachmentKey): AxiosPromise<TaskAttachmentModel> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/flow/task/attachment/id",
      method: "delete",
      params: query,
    });
  }
}
const taskAttachmentApi = new TaskAttachmentApiImpl();

export { taskAttachmentApi };
